#include "CycQueue.hpp" 
#include <sys/types.h>
#include <unistd.h>

void* consumeer(void* args)
{
    cout << "debug-1" << endl; 
    CycQueue<int>* rq =  (CycQueue<int>*)args ;
    while (true)
    {
        sleep(1);
        int x ;  //存储环形队列中返回的数据
        rq->pop(&x);

        cout << "消费者获取数据: " << x << endl ;
    }
    
}

void* productor(void* args)
{
    cout << "debug 2" << endl; 
    CycQueue<int>* rq =  (CycQueue<int>*)args ;
    while(true)
    {
        sleep(1);
        int x = rand() % 99 ;
        rq->push(x);
         cout << "生产者生产数据" << x << endl ; 
    }

}


 int main()
 {
    CycQueue<int>* rq = new CycQueue<int>();  // 为啥这样写呢 ？ 

    pthread_t c[3];
    pthread_t p[2];

    pthread_create(c,nullptr,consumeer,(void*)rq);
    pthread_create(c+1,nullptr,consumeer,(void*)rq);
    pthread_create(c+2,nullptr,consumeer,(void*)rq); 

    pthread_create(p,nullptr,productor,(void*)rq);
    pthread_create(p+1,nullptr,productor,(void*)rq);

    cout << "debug" << endl; 


    for(int i = 0 ; i < 3 ; i++) pthread_join(c[i] , nullptr);
    for(int i = 0 ; i < 2 ; i++) pthread_join(p[i] , nullptr);

    delete rq ;
    return 0 ; 
 }


 